/**
 Gulpfile for peccancy
 created by dengyq
 @违章模块打包
 */

var gulp = require('gulp'),
    os = require('os'),
    gutil = require('gulp-util'),
    less = require('gulp-less'),
    concat = require('gulp-concat'),
    gulpOpen = require('gulp-open'),
    uglify = require('gulp-uglify'),
    cssmin = require('gulp-cssmin'),
    md5 = require('gulp-md5-plus'),
    fileinclude = require('gulp-file-include'),
    clean = require('gulp-clean'),
    spriter = require('gulp-css-spriter'),
    base64 = require('gulp-css-base64'),
    webpack = require('webpack'),
    webpackConfig = require('./webpack.config.js'),
    connect = require('gulp-connect'),
    runSequence=require('gulp-sequence');

var host = {
    path: 'web/',
    port: 8089,
    html: 'index.html'
};

//mac chrome: "Google chrome",
var browser = os.platform() === 'linux' ? 'Google chrome' : (
    os.platform() === 'darwin' ? 'Google chrome' : (
        os.platform() === 'win32' ? 'chrome' : 'firefox'));
var pkg = require('./package.json');

//将图片拷贝到目标目录
gulp.task('copy:images', function (done) {
    gulp.src(['static/images/**/*']).pipe(gulp.dest('web/images')).on('end', done);
});

//将原来文件拷贝到目标目录
gulp.task('copy:files', function (done) {
    gulp.src('web_old/**/*',{base:'web_old'}).pipe(gulp.dest('web')).on('end', done);
});

//将html拷贝到目标目录
gulp.task('copy:html', function (done) {
    gulp.src(['static/**/*.html'],{base:'static'}).pipe(gulp.dest('web')).on('end', done);
});
//将图标文件拷贝到目标目录
gulp.task('copy:icon', function (done) {
    gulp.src(['static/icon/**/*']).pipe(gulp.dest('web/icon')).on('end', done);
});
//压缩合并css, css中既有自己写的.less, 也有引入第三方库的.css
gulp.task('cssmin', function (done) {
    gulp.src(['static/css/*.css'])
        .pipe(concat('main.css'))
        .pipe(gulp.dest('web/css/'))
        .on('end', done);
});
//将js加上10位md5,并修改html中的引用路径，该动作依赖build-js
gulp.task('md5:js', ['build-js'], function (done) {
    gulp.src('web/js/*.js')
        .pipe(uglify())    //压缩
        .pipe(md5(10, ['web/**/*.html']))
        .pipe(gulp.dest('web/js'))
        .on('end', done);
});

//将css加上10位md5，并修改html中的引用路径
gulp.task('md5:css', function (done) {
    gulp.src('web/css/*.css')
        .pipe(cssmin())
        .pipe(md5(10, ['web/**/*.html']))
        .pipe(gulp.dest('web/css'))
        .on('end', done);
});

gulp.task('watch', function (done) {
    gulp.watch(['static/**/*'], ['copy:html','copy:images','cssmin', 'build-js'])
        .on('end', done);
});

gulp.task('connect', function () {
    connect.server({
        root: host.path,
        port: host.port,
        livereload: true
    });
});

gulp.task('open', function (done) {
    gulp.src('')
        .pipe(gulpOpen({
            app: browser,
            uri: 'http://localhost:8089/'
        }))
        .on('end', done);
});

var myDevConfig = Object.create(webpackConfig);

var devCompiler = webpack(myDevConfig);

//引用webpack对js进行操作
gulp.task("build-js", function(callback) {
    devCompiler.run(function(err, stats) {
        if(err) throw new gutil.PluginError("webpack:build-js", err);
        gutil.log("[webpack:build-js]", stats.toString({
            colors: true
        }));
        callback();
    });
});


//发布
gulp.task('default',function(done){
    runSequence(['connect'],['copy:icon'],['copy:html'],['copy:images'],['copy:files'], ['cssmin'],['md5:css'], ['md5:js'],done);
});


//开发
gulp.task('dev',function(done){
    runSequence(['connect'],['copy:icon'],['copy:html'],['copy:images'],['copy:files'], ['cssmin'], ['build-js'],['watch'],done);
});

